<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//

/**
 * Contains class Ajax_Ctrl
 * @package modules
 * @subpackage ajax_module
 */

/**
 *
 */
require_once(TANGRA_MAIN_DIR.'web_site/web_ctrl.class.php');

/**
 *
 */
require_once(TANGRA_MAIN_DIR.'web_site/tple_exports.class.php');
/**
 *
 */
require_once(TANGRA_MAIN_DIR.'exceptions/tangra_module_exception.class.php');
/**
 *
 */
require_once($WSC->get_site_inc_dir().'modules/tple_html_gatherer/tple_html_gatherer.class.php');
/**
 *
 */
require_once($WSC->get_site_inc_dir().'modules/json_encdec_common/json_encdec.class.php');

/**
 * Base class for all ajax controllers
 *
 */
abstract class Ajax_Ctrl extends Web_Ctrl {
	/**
	 * Status code for error
	 *
	 */
	const STATUS_ERROR = 0;

	/**
	 * Status code if everything is ok
	 *
	 */
	const STATUS_OK = 1;

	/**
	 * Command that instructs calling javascript to show the result
	 *
	 */
	const COMMAND_SHOW = 'show';

	/**
	 * Command that instructs calling javascript to perform window.location (redirect)
	 *
	 */
	const COMMAND_REDIRECT = 'redirect';


	/**
	 * path to template file
	 *
	 * @var string
	 * @internal
	 */
	private $tpl_file;

	/**
	 * Holds array for additional response parameters that can be sent back to callic javascript
	 *
	 * @var array
	 * @internal
	 */
	private $additional_response_parameters  = array();



	/**
	 * Constructor
	 *
	 * @param string $tpl_file Path to template file
	 */
	function __construct($tpl_file = '') {
		$this->tpl_file = $tpl_file;
		parent::__construct();
	}


	/**
	 * Method that will be called by the site instance to run the controller
	 *
	 * Not intended to be used directly by user except in vary rare circumstances.
	 *
	 * @internal
	 */
	public function _run() {
		$this->set_transit_vars_as_output_rewrite_vars();
		$_transit_vars_prepared = $this->prepare_transit_vars();
		$this->export('_transit_vars', $_transit_vars_prepared, true);

		$this->run();
	}

	/**
	 * Sets tpl_file
	 *
	 * @param string $tpl_file Path to tplfile
	 */
	protected function set_tpl_file($tpl_file) {
		$this->tpl_file = $tpl_file;
	}


	/**
	 * Returns path to template file
	 *
	 * @return string
	 */
	public function get_tpl_file() {
		return $this->tpl_file;
	}


	/**
	 * Sends back response to the calling javascript
	 *
	 * @param array $json Array of $key => $value pairs that will be send back as JSON header
	 * @param string $html Generated HTML. Default is '' in case of redirect for example.
	 */
	protected function send_response($json, $html = ' ') {

		foreach($this->additional_response_parameters as $key => $value) {
			$json[$key] = $value;
		}

		$json_enc = JSON_EncDec::encode($json);

		global $WSC;
		header('Content-Type: text/html; charset='.$WSC->get_default_encoding());
		header('Cache-control: no-cache');
		header('X-JSON: '.$json_enc);
		print($html);
		flush();
	}


	/**
	 * Adds additional response parameter
	 *
	 * @param string $var_name
	 * @param mixed $value
	 */
	public function add_additional_response_parameter($var_name, $value) {
		if (!array_key_exists($var_name, $this->additional_response_parameters)) {
			$this->additional_response_parameters[$var_name] = $value;
		} else {
			throw new Tangra_Module_Exception('Parameter already added: '.$var_name);
		}
	}


	/**
	 * Alias of add_additional_response_parameter()
	 *
	 * @param string $var_name
	 * @param mixed $value
	 */
	public function add_arp($var_name, $value) {
		$this->add_additional_response_parameter($var_name, $value);
	}


	/**
	 * Adds multiple additional response parameters.
	 *
	 * @param array $additional_json_params Array must be simple $key => $value pairs
	 */
	public function add_additional_response_parameters($additional_response_parameters) {
		if (is_array($additional_response_parameters)) {
			foreach($additional_response_parameters as $key => $value) {
				$this->add_additional_response_parameter($key, $value);
			}
		}
	}


	/**
	 * Returns additional response parameters
	 *
	 * @return array
	 */
	protected function get_additional_response_parameters() {
		return $this->additional_response_parameters;
	}

	/**
	 * Alias of get_additional_response_parameters
	 *
	 * @return array
	 */
	protected function get_arp() {
		return $this->get_additional_response_parameters();
	}


	/**
	 * Parses the template file and returns generated HTML
	 *
	 * @return string
	 */
	protected function get_html() {
		if ($this->get_tpl_file()) {
			$html_gatherer = new Tple_HTML_Gatherer($this->get_tpl_file());

			$ret = $html_gatherer->get_html($this->get__exports());
			$ret = $this->rewrite_html_with_transit_vars($ret);
		} else {
			$ret = '';
		}


		return $ret;
	}


	protected function rewrite_html_with_transit_vars($html) {
		if ($this->get_transit_vars()) {
			ob_start();
			$this->set_transit_vars_as_output_rewrite_vars();
			print($html);
			$ret = ob_get_contents();
			ob_end_clean();
		} else {
			$ret = $html;
		}

		return $ret;
	}
}